//
//  CropIntroduceViewController.m
//  AnXin100
//
//  Created by Sakura on 2018/11/12.
// /Users/wangherun/anxin100_ios/AnXin100/AnXin100.xcodeproj Copyright © 2018 tzhg. All rights reserved.
//

#import "CropIntroduceViewController.h"
#import "XXPageTabView.h"
#import "XXPageTabItemLable.h"
#import "CropViewController.h"
#import "UIViewController+HBD.h"

@interface CropIntroduceViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) XXPageTabView *pageTabView;

@property (nonatomic, strong) UIScrollView *mainScrollView;

@end

@implementation CropIntroduceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    /** 导航条背景颜色 */
    self.hbd_barTintColor = [UIColor whiteColor];
    /** 去掉导航条线 */
    self.hbd_barShadowHidden = YES;
    
    [self setupNavigationItems];
    
    NSArray *titleAry = @[@"简介",@"水肥",@"植保",@"栽培", @"仓储", @"基地"];
    for (NSInteger i = 0; i < titleAry.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(SCREEN_WIDTH / 6 * i, Hn, SCREEN_WIDTH / 6, 30);
        [btn setTitle:titleAry[i] forState:UIControlStateNormal];
        [btn setTitleColor:COLOR_Gray forState:UIControlStateNormal];
        [btn setTitleColor:COLOR_GREEN forState:UIControlStateSelected];
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        btn.tag = 100 + i;
        if (i == 0) {
            btn.selected = YES;
        }
        [self.view addSubview:btn];
        [btn addTarget:self action:@selector(clickAciton:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, Hn + 40, SCREEN_WIDTH, 150)];
    imageView.image = [UIImage imageNamed:@"crop"];
    [self.view addSubview:imageView];
    
    [self.view addSubview:self.mainScrollView];
}


//自定义UIBarButtonItem
- (void)setupNavigationItems {
    
    /** 导航条左边的按钮 */
    //返回按钮
    UIButton *kfBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [kfBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    kfBtn.frame = CGRectMake(0, 0, 60, 30);
    [kfBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:kfBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    
   
    /** 搜索 */
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
    titleLabel.text = @"葡萄";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = FONT_EIGHTEEN;
    titleLabel.textColor = [UIColor colorWithHex:0x29b566];
    self.navigationItem.titleView = titleLabel;
    
    
}

#pragma mark - Action

- (void)backAction:(UIBarButtonItem *)item {
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (UIScrollView *)mainScrollView {
    
    if (!_mainScrollView) {
        _mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, Hn + 190, SCREEN_WIDTH, SCREEN_HEIGHT - Hn - 190)];
        _mainScrollView.contentSize = CGSizeMake(SCREEN_WIDTH * 6, 0);
        _mainScrollView.pagingEnabled = YES;
        _mainScrollView.delegate = self;
        for (NSInteger i = 0 ; i < 6; i++) {
            CropViewController *vc = [[CropViewController alloc] init];
            [self addChildViewController:vc];
            vc.view.mj_x = SCREEN_WIDTH * i;
            [_mainScrollView addSubview:vc.view];


            if (i == 0 || i == 4 || i == 5) {
                vc.isShow = YES;
            } else {
                vc.isShow = NO;
            }

            
        }
    }
    return _mainScrollView;
}

- (void)clickAciton:(UIButton *)sender {
    
    sender.selected = YES;
    for (NSInteger i = 0; i < 6; i++) {
        UIButton *button = [self.view viewWithTag:100 + i];
        if (button.tag != sender.tag) {
            button.selected = NO;
        }
    }
    [_mainScrollView setContentOffset:CGPointMake(SCREEN_WIDTH * (sender.tag - 100), 0) animated:YES];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger i = scrollView.contentOffset.x / SCREEN_WIDTH;
    [self clickAciton:[self.view viewWithTag:100 + i]];
}

@end
